package com.school.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.school.entity.HljtClass;
import com.school.entity.HljtExam;
import com.school.entity.web.Examination;
import com.school.service.HljtOtherInterfacesService;
import com.school.service.impl.HljtClassServiceIpml;
import com.school.service.impl.HljtExamServiceImpl;
import com.school.util.Message;
import com.school.util.RepUtil;

@Controller
public class HljtExamController {
	
	@Autowired
	private HljtClassServiceIpml hljtClassServiceIpml;
	@Autowired
	private HljtExamServiceImpl hljtExamServiceImpl;
	@Autowired
	HljtOtherInterfacesService hljtOtherInterfacesService;
	
	/**
	 * 考试模式查询
	 * @param classid
	 * @param pageSize
	 * @param pageIndex
	 * @param rep
	 * @return
	 */
	@RequestMapping("/selectexamination")
	@ResponseBody
	public Message selectExamination(Integer classid,Integer pageSize,Integer pageIndex,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		
		if(null !=classid) {
			if(classid>-1) {
				pageIndex=(pageIndex-1)*pageSize;
				List<HljtClass> HljtClasses=hljtClassServiceIpml.pageFindClassList(classid, null, null,null);
				List<Examination> examination=hljtExamServiceImpl.findhljtexamall(classid, pageSize, pageIndex);
				List<Examination> examinationall=hljtExamServiceImpl.findhljtexamall(classid, null, null);
				
				data.put("data", examination);
				data.put("Total", examinationall.size());
				data.put("class", HljtClasses);
				data.put("success", true);
			}else {
				classid=null;
				pageIndex=(pageIndex-1)*pageSize;
				List<HljtClass> HljtClasses=hljtClassServiceIpml.pageFindClassList(null, null, null,null);
				List<Examination> examination=hljtExamServiceImpl.findhljtexamall(classid, pageSize, pageIndex);
				List<Examination> examinationall=hljtExamServiceImpl.findhljtexamall(classid, null, null);
				
				
				data.put("data", examination);
				data.put("Total", examinationall.size());
				data.put("class", HljtClasses);
				data.put("success", true);
			}
		}else {
			data.put("success",  false);
		}
		Message.setData(data);
		return Message;
	}
	/**
	 * 修改/添加班级考试模式（共用）
	 * @param classid
	 * @param teachername
	 * @param testname
	 * @param subject
	 * @param starttime
	 * @param endtime
	 * @param content
	 * @param rep
	 * @return
	 */
	@RequestMapping("/updateexamination")
	@ResponseBody
	public Message updateExamination(Integer examClassId , Integer id,Integer classid,String teachername,String testname,String subject,String starttime,String endtime,String content,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		if(null!=id&&null!=teachername&&null!=testname&&null!=subject&&null!=starttime&&null!=endtime&&null!=content&&classid==null) {
			HljtExam hljtexam=new HljtExam();
			hljtexam.setId(id);
			hljtexam.setTeachername(teachername);
			hljtexam.setTestname(testname);
			hljtexam.setSubject(subject);
			hljtexam.setStarttime(starttime);
			hljtexam.setEndtime(endtime);
			hljtexam.setContent(content);
			hljtexam.setFontsize(14);
			
			hljtExamServiceImpl.updateexam(hljtexam);
			hljtOtherInterfacesService.modifyByCalssId(examClassId);
			data.put("success", true);
		}else if(null==id&&null!=teachername&&null!=testname&&null!=subject&&null!=starttime&&null!=endtime&&null!=content&&null != classid){
			HljtExam hljtexam=new HljtExam();
			hljtexam.setClassid(classid);
			hljtexam.setTeachername(teachername);
			hljtexam.setTestname(testname);
			hljtexam.setSubject(subject);
			hljtexam.setStarttime(starttime);
			hljtexam.setEndtime(endtime);
			hljtexam.setContent(content);
			hljtexam.setFontsize(14);
			
			hljtExamServiceImpl.addexam(hljtexam);
			hljtOtherInterfacesService.modifyByCalssId(examClassId);
			data.put("success",  true);
		}else {
			data.put("success",  true);
		}
		Message.setData(data);
		return Message;
	}
	
	
	/**
	 * 删除条数据
	 * @param id
	 * @param rep
	 * @return
	 */
	@RequestMapping("/deleteexamination")
	@ResponseBody
	public Message deleteExamination(Integer examClassId , Integer id,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		if( null !=id) {
			hljtExamServiceImpl.deleteexam(id);
			hljtOtherInterfacesService.modifyByCalssId(examClassId);
			data.put("success", true);
		}else {
			data.put("success",  false);
		}
		Message.setData(data);
		return Message;
		
	}
}
